'use strict'
/**
 * 
 */
angular.module('WDCC').directive('paging',function($timeout) {
	return {
			restrict : 'ACE',
			replace : true,
			require : '?ngModel',
			scope : {
				page : '=pageObject',
				query : '=clickFunction',
				jumpFlag : '=jump',
				firstNum:'=firstNum',
				cached:'=cached'
			},
			link : function($scope,$element,attrs,ngModel) {
				
				
				//pageNo当前第几页,pagesCount总页数,viewPagesCount最大需要显示多少页
				$scope.calcStartPage = function(pageNo,maxPage,viewPages){
					if(maxPage<=viewPages){return 1;}
					else if(pageNo==1){return 1;}
					else if((maxPage-pageNo+1)>=viewPages){return pageNo-1;}
					else{ return (maxPage-viewPages+1); }
				}
				$scope.calcPageNo = function(pageNo){
					var addNum = 1;
					if($scope.firstNum!=null){addNum= 1-$scope.firstNum;}
					return pageNo-addNum;
				}
				$scope.createHtml = function() {
					$element.html("");
					//默认后台的第一页为0,这里要补1
					var addNum = 1;
					if($scope.firstNum!=null){addNum= 1-$scope.firstNum;}
					//最多显示多少个页数,默认7页
					var viewPages = 7;
					if($scope.page.viewPages!=null){
						viewPages=$scope.page.viewPages;
					}
					//计算总页数
					var maxPage = Math.ceil($scope.page.total / $scope.page.pageSize);
					//显示的值按1算
					//$scope.page.pageNum为后台的值,实际有可能为0也可能为1,如果为0要补1
					var pageNo = $scope.page.pageNum + addNum;
					var startPage = $scope.calcStartPage(pageNo,maxPage,viewPages);
					var endPage = (maxPage<=viewPages)?maxPage:(startPage+viewPages-1);
					if(maxPage>1){
						var str = '<ul class="pagination success">';
						if(startPage>1){
							str += '<li><a href="javascript:void(0);" inx="'+(startPage-1)+'">«</a></li>';
						}
						for (var i = startPage; i <= endPage; i++) {
							if(pageNo == i){
								str += '<li class="active"><a href="javascript:void(0);" inx="'+i+'">'+i+'</a></li>' ;
							}else{
								str += '<li><a href="javascript:void(0);" inx="'+i+'">'+i+'</a></li>' ;
							}
						}
						if(endPage<maxPage){
							str += '<li><a href="javascript:void(0);" inx="'+(endPage+1)+'">»</a></li>';
						}
						str += '</ul>';
						if($scope.jumpFlag!=null&&$scope.jumpFlag&&maxPage>viewPages){
							str += '<div class="pagination success"><div class="pagination success"><input type="number" min="1"><button type="button">跳转</button></div></div>';
						}
						$element.html(str);
						$scope.bindEvent();
					}
				};
				//$scope.query在服务端分页后返回数据
				//$scope.splicedata将服务器返回的数据在客户端分页
				$scope.queryData = function(){
					if($scope.cached){
						$scope.splicedata();
					}else{
						$scope.query();
					}
				}
				//将远程的值放在前端进行分页,先分好页之后再按页码取值
				$scope.splicedata = function(){
					if($scope.page.data==null){
						return;
					}else{
						var startNo = $scope.page.pageNum * $scope.page.pageSize;
						var endNo = startNo + $scope.page.pageSize;
						if(endNo>=($scope.page.total-1))endNo = $scope.page.total;
						ngModel.$setViewValue($scope.page.data.slice(startNo,endNo));
					}
				}
				
				/**绑定了监听事件**/
				$scope.bindEvent = function() {
					$element.find('a').on('click', function() {
						var text = $(this).attr("inx");
						$scope.page.pageNum = $scope.calcPageNo(parseInt(text));
						$scope.queryData();
						$scope.createHtml();
					});
					$element.find('button').on('click', function() {
						var text = $element.find('input')[0];
						if(text!=null){
							var maxPage = Math.ceil($scope.page.total / $scope.page.pageSize);
							var pageNo = parseInt(text.value);
							if(pageNo>=1&&pageNo<=maxPage){
								$scope.page.pageNum =$scope.calcPageNo(pageNo);
								$scope.queryData();
								$scope.createHtml();
							}
						}
					});
				}
				$scope.createHtml();
				$scope.$watch('page.total', function() {
					//如果客户端分页,在生成分页标签时,同时获取第一页数据
					if($scope.cached){
						$scope.splicedata();
					}
					$scope.createHtml();
				});
				
				$scope.$watch('page.pageSize', function(newValue,oldValue,scope) {
					if(oldValue!=newValue){
						$scope.page.pageNum = ($scope.firstNum!=null)?$scope.firstNum:0;
						$scope.page.total = 0;
						$scope.queryData();
						$scope.createHtml();
					}
				});
			}
		}
});